package cn.tradewin.advisor.pages.workflow;

import java.io.IOException;
import java.io.InputStream;
import java.util.Collections;
import java.util.List;

import org.activiti.bpmn.model.BpmnModel;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.impl.cfg.StandaloneProcessEngineConfiguration;
import org.activiti.engine.impl.context.Context;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.image.ProcessDiagramGenerator;
import org.activiti.image.impl.DefaultProcessDiagramGenerator;
import org.apache.tapestry5.ComponentResources;
import org.apache.tapestry5.Link;
import org.apache.tapestry5.StreamResponse;
import org.apache.tapestry5.ioc.Messages;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.apache.tapestry5.ioc.annotations.InjectService;
import org.apache.tapestry5.services.Response;

import cn.tradewin.advisor.exceptions.DBException;

public class ProcessStatus {
	
	@Inject
	private RuntimeService runtimeService;

	@Inject
	private RepositoryService repositoryService;

	@Inject
	private Messages messages;
	
	@InjectService("ProcessEngineConfiguration")
	private StandaloneProcessEngineConfiguration processEngineConfiguration;
	
	@Inject
	private ProcessEngine processEngine;
	
	@Inject
	private ProcessDiagramGenerator processDiagramGenerator;

	private String processid;
	
	@Inject
	private ComponentResources _resources;

	public Link getChart() {
		return _resources.createEventLink("chart", false, null);
	}
	
	void onActivate(String processid) throws DBException {
		this.processid = processid;
	}

	String onPassivate() {
		return processid;
	}
	
	StreamResponse onChart() {
		return new StreamResponse() {
			public String getContentType() {
				return "image/png";
			}
			public InputStream getStream() throws IOException {
				try {
					
					ProcessInstance processInstance = runtimeService.createProcessInstanceQuery().processInstanceId(processid).singleResult();
					if (processInstance == null) return null;
					
					BpmnModel bpmnModel = repositoryService.getBpmnModel(processInstance.getProcessDefinitionId());
					
					// current execute node
				    List<String> activeActivityIds = runtimeService.getActiveActivityIds(processid);
				    
				    // add for test begin
				    return new DefaultProcessDiagramGenerator().generateDiagram(
				    		bpmnModel, "png",
				    		activeActivityIds, Collections.<String>emptyList(), 
				    		processEngine.getProcessEngineConfiguration().getActivityFontName(), 
				    		processEngine.getProcessEngineConfiguration().getLabelFontName(), 
				    		null, 1.0);
				    
				    // add for test end
				    
				    
				    
				    
				    
//				    Context.setProcessEngineConfiguration(processEngineConfiguration);
//				    
//				    return processDiagramGenerator.generateDiagram(bpmnModel, "png", activeActivityIds);

				} catch (Exception ex) {
					throw new RuntimeException("Cannot read image file.", ex);
				}
			}
			public void prepareResponse(Response response) {
			}
		};
	}	
}
